package com.iot.inclinometer.util;

public class Matrix {
	/*矩阵相乘*/
	public static double[][] Mult(double mult1[][], double mult2[][]){
		double[][] mult = new double[mult1.length][mult2[0].length];
		for(int i = 0; i < mult1.length; i++){
			for(int j = 0; j < mult2[0].length; j++){
				double sum = 0;
				for(int m = 0; m < mult2.length; m++){
					double temp = 0;
					temp = mult1[i][m]*mult2[m][j];
					sum += temp;
				}
				mult[i][j] = sum;
			}
		}
		return mult;
	}
	/** 行列式求值, 高斯-约旦消元法*/
	private static double MatValue(double[][] temp1) {
		
		double[][] in = new double[temp1.length][temp1[0].length];
		
		for(int m = 0; m < in.length; m++){
			for(int n = 0; n < in[0].length; n++){
				in[m][n] = temp1[m][n];
			}
		}
		double value = 0;
		if (in.length == 2) {
			value = in[0][0] * in[1][1] - in[0][1] * in[1][0];
		}
		else {
			for (int i = 0; i < in[0].length; i++) {
				double[][] temp = Alge(in, 0, i);
				value = (double) (value + in[0][i]*Math.pow(-1, 0+i)*MatValue(temp));
			}
		}
		return value;
	}
	
	/*返回代数余子式*/
	private static double[][] Alge(double[][] alge, int row, int col) {
		double[][] temp = new double[alge.length][alge[0].length];
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {
				temp[i][j] = alge[i][j];
			}
		}
		double[][] cofact = new double[temp.length - 1][temp[0].length - 1];
		if (row > 0) {
			for (int i = row - 1; i >= 0; i--) {
				for (int j = 0; j < temp[0].length; j++) {
					temp[i + 1][j] = temp[i][j];
				}
			}
		}
		if (col > 0) {
			for (int i = col - 1; i >= 0; i--) {
				for (int j = 0; j < temp.length; j++) {
					temp[j][i + 1] = temp[j][i];
				}
			}
		}
		for (int i = 1; i < temp.length; i++) {
			for (int j = 1; j < temp[0].length; j++) {

				cofact[i - 1][j - 1] = temp[i][j];
			}
		}
		return cofact;
	}

	/*伴隨矩阵*/
	private static double[][] Adjoint(double[][] adjoint) {
		double[][] temp = new double[adjoint.length][adjoint[0].length];
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {		
				temp[i][j] = (double) (Math.pow(-1, i+j)*MatValue(Alge(adjoint, i, j)));
			}
		}
		return temp;
	}
	/*逆矩阵*/
	public static double[][] Inver(double[][] inver) {
		double[][] temp = new double[inver.length][inver[0].length];
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {
				temp[i][j] = inver[i][j];
			}
		}
		double[][] adj = Adjoint(temp);
		double[][] inv = new double[temp.length][temp[0].length];
		double abs_inv = MatValue(temp);
		for (int i = 0; i < inv.length; i++) {
			for (int j = 0; j < inv[0].length; j++) {
				inv[i][j] = adj[i][j] / abs_inv;
			}
		}
		return inv;
	}
	/*转置*/
	public static double[][] Turn(double turn[][]) {

		double[][] temp = new double[turn[0].length][turn.length];
		
		for (int i = 0; i < turn[0].length; i++) {
			for (int j = 0; j < turn.length; j++) {
				temp[i][j] = turn[j][i];
			}
		}
		
		return temp;
	}
	 public static double[][] div(double[][] matrix_a,double[][] matrix_b){
		 int arow = matrix_a.length;
		 int acol = matrix_a[0].length;
		 int brow = matrix_b.length;
		 int bcol = matrix_b[0].length;
		  if(acol != brow){
			 System.out.println("请输入正确格式的矩阵");
			 return null;
		 }
		 else{
			 double[][] result = new double[arow][bcol];
			 for(int i=0;i<arow;i++){
				 for(int j=0;j<acol;j++){
					 for(int k=0;k<brow;k++){
						 result[i][j]+=matrix_a[i][k]*matrix_b[k][j];
					 }
				 }
			 }
			 return result;	
		 }
	 }
}
